#include "gofgame/dungeonkeeper/ui/SkillElementView.h"
#include "gofgame/common/ui/MessageDialog.h"
#include "gofgame/common/ui/TableLayout.h"
#include "gofgame/common/ui/TableLayoutData.h"
#include "gofgame/common/ui/TableColumn.h"
#include "gofgame/common/graphics/HornedTextureBrush.h"
#include "gofgame/common/graphics/LinearGradientBrush.h"
#include "gofgame/dungeonkeeper/ui/MessageViewDialog.h"

using namespace gofgame::dungeonkeeper::ui;

HornedTextureBrushResource SkillElementView::elementBrushResource(20, 20, "gofgame/dungeonkeeper/ui/element.png");

SkillElementView::SkillElementView() {
	this->image.initialize();
	this->descriptionLabel.initialize();
	this->upgradeButton.initialize();
	this->levelLabel.initialize();
	this->coolDownLabel.initialize();
	this->id = 0;
	this->unlock = 0;
}

bool SkillElementView::init() {
	if (!Container::init()) {
		return false;
	}
	
	this->setBackgroundBrush(elementBrushResource.unwrap());
	this->setPadding(20);
	TableLayout *tableLayout = new TableLayout(35);
	this->setHeight(
		tableLayout->getMinRowHeight() * 2 + 
		this->getPaddingTop() + 
		this->getPaddingBottom());

	tableLayout->setCellPadding(2);
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::absolute(150));
	this->setLayout(tableLayout);

	TableLayoutData *tableLayoutData;
	this->image->init();
	this->addChild(this->image);
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setRowSpan(2);
	this->image->setLayoutData(tableLayoutData);

	this->descriptionLabel->init();
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setColSpan(6);
	this->descriptionLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->descriptionLabel->setLayoutData(tableLayoutData);
	this->addChild(this->descriptionLabel);

	this->upgradeButton->init();
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setRowSpan(2);
	this->upgradeButton->setLayoutData(tableLayoutData);
	this->upgradeButton->setBackgroundBrush(
		new HornedTextureBrush(
			CCSize(10, 10),
			CCTextureCache::sharedTextureCache()->addImage(
				"gofgame/dungeonkeeper/ui/upgrade_button.png"
			),
			Brush::white()
		)
	);
	this->upgradeButton->setPressedBackgroundBrush(
		new HornedTextureBrush(
			CCSize(10, 10),
			CCTextureCache::sharedTextureCache()->addImage(
				"gofgame/dungeonkeeper/ui/upgrade_button.png"
			),
			new LinearGradientBrush(0, 0, 0, 0, 1, 0, 0, 1)
		)
	);
	this->upgradeButton->click() += event_handler<ComponentEventArgs>(
		this, &SkillElementView::upgradeButton_click);
	this->addChild(this->upgradeButton);

	Ref<Label> levelDescriptionLabel;
	levelDescriptionLabel.initialize();
	levelDescriptionLabel->init();
	levelDescriptionLabel->setText("Level: ");
	levelDescriptionLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(levelDescriptionLabel);
	this->levelLabel->init();
	this->levelLabel->setText(1);
	this->levelLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->levelLabel);

	Ref<Label> menaDescriptionLabel;
	menaDescriptionLabel.initialize();
	menaDescriptionLabel->init();
	menaDescriptionLabel->setText("Mana: ");
	menaDescriptionLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(menaDescriptionLabel);
	this->menaLabel.initialize();
	this->menaLabel->init();
	this->menaLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->menaLabel);

	Ref<Label> coolDownDescriptionLabel;
	coolDownDescriptionLabel.initialize();
	coolDownDescriptionLabel->init();
	coolDownDescriptionLabel->setText("CoolDown: ");
	coolDownDescriptionLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(coolDownDescriptionLabel);
	this->coolDownLabel->init();
	this->coolDownLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->coolDownLabel);

	return true;
}

void SkillElementView::upgradeButton_click(void *sender, ComponentEventArgs &e) {
	Ref<MessageDialog> dialog;
	dialog.initialize(new MessageViewDialog(MessageDialogType_OKCancel));
	dialog->init();
	dialog->setTitle("Are you sure...");
	dialog->setMessage("Are you sure to upgrade this skill?");
	dialog->closed() += event_handler<DialogClosedEventArgs>(
		this, &SkillElementView::upgradeConfirmDialog_close);
	dialog->show();
}

void SkillElementView::upgradeConfirmDialog_close(void* sender, DialogClosedEventArgs &e) {
	if (e.getDialogResult() == DialogResult_OK) {
		SkillUpgradeEventArgs skillUpgradeEventArgs(this, this->getSkillElementViewInfo());
		this->fireManualBubbleEvent(skillUpgradeEventArgs);
		this->setSkillElementViewInfo(skillUpgradeEventArgs.getSkillElementViewInfo());
	}
}

SkillElementViewInfo SkillElementView::getSkillElementViewInfo() const {
	SkillElementViewInfo sevi;
	sevi.imgPath = this->image->getResourceName();
	sevi.desc = this->descriptionLabel->getText();
	sevi.id = this->id;
	sevi.unlock = this->unlock == 1?true:false;
	sscanf(this->coolDownLabel->getText(), "%f", &sevi.coolDown);
	sscanf(this->levelLabel->getText(), "%d", &sevi.lv);
	sscanf(this->menaLabel->getText(), "%d", &sevi.mana);
	return sevi;
}

void SkillElementView::setSkillElementViewInfo(const SkillElementViewInfo &sevi){
	char text[32];
	this->image->setResourceName(sevi.imgPath.c_str());
	this->descriptionLabel->setText(sevi.desc.c_str());
	sprintf(text, "%f", sevi.coolDown);
	this->coolDownLabel->setText(text);
	sprintf(text, "%d", sevi.lv);
	this->levelLabel->setText(text);
	sprintf(text, "%d", sevi.mana);
	this->menaLabel->setText(text);

	this->id = sevi.id;
	this->unlock = sevi.unlock;
}
